package mysql

import (
	"box-fresh/boxfreshrpc/boxfreshrpc"

	"gorm.io/gorm"
)

// 商品

type Shop struct {
	gorm.Model
	Name  string  `gorm:"type:varchar(30);unique;not null;comment:商品名称"`
	Style string  `gorm:"type:varchar(30);not null;comment:商品类型"`
	Price float64 `gorm:"type:float;not null;comment:商品价格"`
}

// 商品添加

func (s *Shop) ShopAdd(db *gorm.DB) error {
	return db.Debug().Create(s).Error
}

// 商品查询

func (s *Shop) ShopSelect(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Find(s).Error
}

// 商品列表

func ShopList(db *gorm.DB, in *boxfreshrpc.ShopListRequest) []*boxfreshrpc.ShopList {
	var list []*boxfreshrpc.ShopList
	db.Debug().Model(&Shop{}).
		Select("shops.id as shop_id", "name", "style", "price").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&list)
	return list
}
